<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      :use-page="false"
    >
      <template #default>
        <el-table-column label="人员" align="center" width="500">
          <template #default="{ row }">
            {{ row.configNameList.map((item) => item.signedName).join(",") }}
          </template>
        </el-table-column>
      </template>
      <template #actionButton="{ row }">
        <el-button type="text" icon="Edit" @click="openRenyuan(row)"
          >配置</el-button
        >
      </template></Table
    >
    <el-dialog v-model="outerVisible" width="60%" title="配置人员">
      <el-input v-model="userNames" readonly @click="onInnerOpen()"></el-input>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="outerVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit"> 确 认 </el-button>
        </span>
      </template>
      <TableSelect
        ref="jkTableSelect"
        :queryApi="listUser"
        :options="[]"
        :rowKey="'userId'"
        rowLabel="nickName"
        @onConfirm="onJkConfirm"
        rowLabelText="用户昵称"
      >
        <template #formSlot="{ handleQuery, filterForm }">
          <el-col :span="8">
            <el-form-item label="部门名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.deptName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.userName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #tableSlot>
          <el-table-column
            label="部门"
            align="center"
            key="deptName"
            prop="dept.deptName"
          />
          <el-table-column
            label="用户名称"
            align="center"
            key="userName"
            prop="userName"
          />
          <el-table-column
            label="用户昵称"
            align="center"
            key="nickName"
            prop="nickName"
          />
        </template>
      </TableSelect>
    </el-dialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/zygl/zylcpz.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJsonNew } from "@/views/hooks/useAsyncData.js";
import TableSelect from "@/components/tableSelect/index";
import { listUser } from "@/api/system/user";
const { proxy } = getCurrentInstance();

const ops = useFormJsonNew(formJson, {
  sort: [],
  sortable: [],
});

const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["jobType", "linkName", "riskGrade", "izQualified", "cardType"].includes(
      i.prop
    )
  ),
});

const editRow = ref({});
const userNames = ref("");
const outerVisible = ref(false);
const openRenyuan = (row) => {
  editRow.value = row;
  userNames.value = row.configNameList.map((item) => item.signedName).join(",");
  outerVisible.value = true;
};

const showSearch = ref(true);
const jkTableSelect = ref();
const dialogVform = ref();
const onInnerOpen = () => {
  jkTableSelect.value.open(
    editRow.value.configNameList.map((item, index) => ({
      value: item.signedBy,
      label: item.signedName,
    }))
  );
};
const onJkConfirm = (data) => {
  userNames.value = data.map((item) => item.label).join(",");
  editRow.value.configNameList = data.map((item) => ({
    signedBy: item.value,
    signedName: item.label,
  }));
};

const onSubmit = () => {
  APIS.update(editRow.value).then((res) => {
    getList();
    outerVisible.value = false;
  });
};
const table = ref();
const { fetchData, getList, queryParams, form } = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
});
</script>

<style lang="scss" scoped></style>
